package com.longqinuo.mycarservice.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;



import com.longqinuo.mycarservice.MainActivity;
import com.longqinuo.mycarservice.R;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private int mGuideImages[] = {R.mipmap.guide1, R.mipmap.guide2,R.mipmap.guide3,R.mipmap.guide4};
    private List<View> mList;
    private ImageView mImageItem;
    private Button mNext;
    private Intent mIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_main);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        initList();
        initView();
    }

    private void initList() {
        mList = new ArrayList<>();
        for (int i = 0; i < mGuideImages.length; i++) {
            View view = View.inflate(this,R.layout.guide_item,null);
            mImageItem = (ImageView) view.findViewById(R.id.iv_guide_item);
            mImageItem.setImageResource(mGuideImages[i]);
            mList.add(view);
        }

    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                View view = mList.get(position);
                mNext = (Button) view.findViewById(R.id.btn_guide_next);
                mNext.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position == mList.size()-1) {
                            mIntent = new Intent(GuideActivity.this, MainActivity.class);
                            startActivity(mIntent);
                            finish();
                        } else {
                            mViewPager.setCurrentItem(position+1,true);

                        }
                    }
                });
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mList.get(position));
            }
        });


    }



}
